﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using intelliPaper.Model;
using System.IO;

namespace intelliPaper.FileHandler
{
    class Program
    {
        static void Main(string[] args)
        {
            Paper p = new Paper();
        //    Question q1 = new Question();
        //    q1.SetID(1);
        //    Question q2 = new Question();
        //    q2.SetID(2);
        //    Question q3 = new Question();
        //    q3.SetID(3);

        //    KnowledgePoint k1 = new KnowledgePoint();
        //    k1.SetID(122);
        //    KnowledgePoint k2 = new KnowledgePoint();
        //    k2.SetID(234);

        //    p.KnowledgePoints.Add(k2);
        //    p.KnowledgePoints.Add(k1);
        //    p.Questions.Add(q3);
        //    p.Questions.Add(q2);
        //    p.Questions.Add(q1);

        //    p.Difficulty = 4.4;
        //    p.Introduction = "用于2011-2012学年度下学期期末考试";
        //    p.LastModifyDate = DateTime.Now;
        //    p.TestDate = new DateTime(2012, 6, 30);

        //    MPDHandler.WriteToMPDFile(p, "paper_temp.mpd");

            p = MPDHandler.ReadFromMPDFile("paper_temp.mpd");
        }
    }


    public class MPDHandler
    {
        static public bool WriteToMPDFile(Model.Paper paper, string path)
        {
            if (paper == null)
                throw new ArgumentNullException();
            try
            {
                StringBuilder sb = new StringBuilder("$.mpd");
                sb.AppendFormat("${0}${1}${2}${3}${4}${5}${6}${7}${8}$", "LastModifyDate", paper.LastModifyDate, "Introduction", paper.Introduction, "TestDate", paper.TestDate, "Difficulty", paper.Difficulty, "Questions");
                foreach (Question question in paper.Questions)
                {
                    sb.AppendFormat("{0} ", question.ID);
                }
                //sb.AppendFormat("${0}$", "KnowledgePoints");
                //foreach (KnowledgePoint knowledgepoint in paper.KnowledgePoints)
                //{
                //    sb.AppendFormat("{0} ", knowledgepoint.ID);
                //}

                FileStream fs = new FileStream(path, FileMode.Create );
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(sb.ToString());
                sw.Flush();
                sw.Close();
                fs.Close();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }

        static public Paper ReadFromMPDFile(string path)
        {
            try
            {
                if (path.Substring(path.Length - 4) != ".mpd")
                    throw new ArgumentException();
                Paper paper = new Paper();
                FileStream fs = new FileStream(path, FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string content = sr.ReadToEnd().Trim ();
                List<string> values = content.Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries).ToList() ;
                if (values.First() != ".mpd")
                    throw new ArgumentException();

                paper.LastModifyDate = DateTime.Parse(values[2].Trim());
                paper.Introduction = values[4].Trim();
                paper.TestDate = DateTime.Parse(values[6].Trim());
                paper.Difficulty = double.Parse(values[8].Trim());

                string[] qs = values[10].Trim().Split(' ');
                foreach (string s in qs)
                {
                    Question q = new Question();
                    q.SetID(int.Parse(s));
                    paper.Questions.Add(q);
                }

                //string[] ks = values[12].Trim().Split(' ');
                //foreach (string s in ks)
                //{
                //    KnowledgePoint k = new KnowledgePoint();
                //    k.SetID(int.Parse(s));
                //    paper.KnowledgePoints .Add(k);
                //}

                return paper;

            }
            catch (ArgumentException)
            {
                Console.WriteLine("无法识别的文件格式");
                return null;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
            
        }
    }
}
